var express = require('express');
var router = express.Router();
var ModelArticle = require('../model/Article');
var bodyParser = require('body-parser');
let md5 = require('js-md5');
// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false })

/* GET users listing. */

router.get('/get_article_list', async function(req, res, next) {

    let article_model  = new ModelArticle();
    try{
        let result = await article_model
            .query('*, article.id AS article_id')
            .join({
                type: false,
                from: {table_name: 'article', column: 'user_id'},
                to:   {table_name: 'users', column: 'id'},
            })
            .search();
        res.end(JSON.stringify({
            servertime: new Date().getTime() / 1000,
            ok: true,
            article_list: result
        }));
    }catch(e){
        throw e;
        res.end(JSON.stringify({
            servertime: new Date().getTime() / 1000,
            ok: false
        }));
    }
});

router.get('/get_article', urlencodedParser , async function(req, res, next) {

    let article_model  = new ModelArticle();
    let id = req.body.id;
    try{
        let result = await article_model.search({
            id,
        });
        res.end(JSON.stringify({
            servertime: new Date().getTime() / 1000,
            ok: true,
            ...result[0]
        }));
    }catch(e){
        res.end(JSON.stringify({
            servertime: new Date().getTime() / 1000,
            ok: false
        }));
    }
});

router.post('/add_article', urlencodedParser , async function(req, res, next) {

    let article_model  = new ModelArticle();
    let user_id = req.body.user_id;
    let title = req.body.title;
    let description = req.body.description;
    let content = req.body.content;
    let create_time = new Date().format('yyyy-MM-dd hh:mm:ss');
    let update_time =  new Date().format('yyyy-MM-dd hh:mm:ss');
    let status;
    try{
        let result = await article_model.add({
            user_id,
            title,
            description,
            content,
            create_time,
            update_time,
        });
        res.end(JSON.stringify({
            servertime: new Date().getTime() / 1000,
            ok: true
        }));
    }catch(e){
        res.end(JSON.stringify({
            servertime: new Date().getTime() / 1000,
            ok: false
        }));
    }
});

router.post('/edit_article', urlencodedParser , async function(req, res, next) {

    let user_model  = new ModelUser();
    let id = req.body.id;
    let sex = req.body.sex;
    let introduce = req.body.introduce;
    let nikename = req.body.nikename;
    let avatar = req.body.avatar;
    let email = req.body.email;
    let phone = req.body.phone;
    let address = req.body.address;
    let status;
    try{
        let result = await user_model.update({
            id,
            sex,
            introduce,
            nikename,
            avatar,
            email,
            phone,
            address,
            age:18
        });
        res.end(JSON.stringify({
            servertime: new Date().getTime() / 1000,
            ok: true
        }));
    }catch(e){
        res.end(JSON.stringify({
            servertime: new Date().getTime() / 1000,
            ok: false
        }));
    }
});

module.exports = router;
